<?php


namespace app\api\controller;


use app\common\service\AdminPhoto;
use app\common\util\Oss;
use think\facade\Request;

class Index
{
    /**
     * 上传图片
     */
    public function uploadImg()
    {
        $file = Request::file();
        $path = 'default';
        try {
            validate(['file'=>[
                'fileSize' => 410241024,
                'fileExt' => 'jpg,jpeg,png,bmp,gif,mp4,pdf,doc,dox,Mp4,MP4'
            ]])->check(['file' => $file]);
        } catch (\think\exception\ValidateException $e) {
            resReturn([], '上传失败: '.$e->getMessage(), 101);
        }
        foreach($file as $k) {
            if(config('web.file-type')==2){
                //阿里云上传
                $res = Oss::alYunOSS($k, $k->extension(),$path);
                if ($res["code"] == 201) resReturn([], '上传失败: '.$res["msg"], 101);
                $name = $res['src'];
            }else{
                $savename = '/'.'upload'. '/'. \think\facade\Filesystem::disk('public')->putFile($path, $k);
                $name = str_replace("\\","/",$savename);
            }
        }
        resReturn($name);
    }

    /**
     * 发送短信
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function sendSms()
    {
        $mobile = input('param.mobile', '');
        if (!$mobile) resReturn('', '手机号码不能为空', 101);
        $code = rand_string(6, 1);
        dataInsert('bl_sms_code', ['mobile' => $mobile, 'sms_code' => $code, 'add_time' => time()]);
        $sms = new Sms();
        $res = $sms->sendSmsCode(strval($mobile), ['code' => $code]);
        if (is_array($res)) {
            if ($res['Code'] == 'OK') {
                resReturn([$code], '发送成功!', 200);
            } else {
                resReturn([], $res['Message'], 101);
            }
        } else {
            resReturn([], $res, 101);
        }
    }


}